package com.flyduck.vueshop.user.h5.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.conditions.update.LambdaUpdateWrapper;
import com.flyduck.vueshop.common.core.exception.BusinessException;
import com.flyduck.vueshop.common.core.utils.BeanUtils;
import com.flyduck.vueshop.common.sotoken.utils.StpUserUtil;
import com.flyduck.vueshop.user.entity.AppAddress;
import com.flyduck.vueshop.user.h5.service.H5AddressService;
import com.flyduck.vueshop.user.h5.vo.H5AddressDetailsRespVO;
import com.flyduck.vueshop.user.h5.vo.H5AddressRespVO;
import com.flyduck.vueshop.user.h5.vo.H5AddressSaveReqVO;
import com.flyduck.vueshop.user.h5.vo.H5AddressUpdateReqVO;
import com.flyduck.vueshop.user.mapper.AppAddressMapper;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.time.LocalDateTime;
import java.util.List;

/**
 * <p>
 * AppAddressServiceImpl
 * </p>
 *
 * @author flyduck
 * @since 2024-06-19
 */
@Service
public class H5AddressServiceImpl implements H5AddressService {

    @Resource
    private AppAddressMapper appAddressMapper;

    @Override
    public List<H5AddressRespVO> getAddressList() {

        List<AppAddress> appAddressList = appAddressMapper.selectList(
                new LambdaQueryWrapper<AppAddress>()
                        .eq(AppAddress::getUserId, StpUserUtil.getLoginIdAsLong())
                        .orderByDesc(AppAddress::getCreated)
        );
        return BeanUtils.toBeanList(appAddressList, H5AddressRespVO.class);

    }

    @Override
    public H5AddressDetailsRespVO getAddressDetailsById(Long id) {
        AppAddress appAddress = appAddressMapper.selectById(id);
        if(appAddress == null || !appAddress.getUserId().equals(StpUserUtil.getLoginIdAsLong()) ){
            throw new BusinessException("收货地址不存在");
        }
        return BeanUtils.toBean(appAddress, H5AddressDetailsRespVO.class);
    }

    @Override
    public void saveAddress(H5AddressSaveReqVO h5AddressSaveReqVO) {
        AppAddress appAddress = BeanUtils.toBean(h5AddressSaveReqVO, AppAddress.class);
        appAddress.setCreated(LocalDateTime.now());
        appAddress.setUserId(StpUserUtil.getLoginIdAsLong());
        // 如果是默认地址，则把以前的默认地址改为非默认地址
        if (h5AddressSaveReqVO.getIsDefault()) {
            appAddressMapper.update(
                new LambdaUpdateWrapper<AppAddress>()
                    .set(AppAddress::getIsDefault, false )
                    .eq(AppAddress::getUserId,StpUserUtil.getLoginIdAsLong())
                    .eq(AppAddress::getIsDefault,true)
            );
        }
        appAddressMapper.insert(appAddress);
    }

    @Override
    public void updateAddress(H5AddressUpdateReqVO h5AddressUpdateReqVO) {
        AppAddress appAddress = BeanUtils.toBean(h5AddressUpdateReqVO, AppAddress.class);
        //校验
        Long count = appAddressMapper.selectCount(
                new LambdaQueryWrapper<AppAddress>()
                        .eq(AppAddress::getUserId, StpUserUtil.getLoginIdAsLong())
                        .eq(AppAddress::getId, h5AddressUpdateReqVO.getId())
        );
        if(count <= 0){
            throw new BusinessException("收货地址不存在");
        }

        // 如果是默认地址，则把以前的默认地址改为非默认地址
        if (h5AddressUpdateReqVO.getIsDefault()) {
            appAddressMapper.update(
                    new LambdaUpdateWrapper<AppAddress>()
                            .set(AppAddress::getIsDefault, false )
                            .eq(AppAddress::getUserId,StpUserUtil.getLoginIdAsLong())
                            .eq(AppAddress::getIsDefault,true)
            );
        }

        appAddress.setUpdated(LocalDateTime.now());
        appAddressMapper.updateById(appAddress);
    }

    @Override
    public void deleteById(Long id) {
        appAddressMapper.delete(
                new LambdaQueryWrapper<AppAddress>()
                .eq(AppAddress::getUserId,StpUserUtil.getLoginIdAsLong())
                .eq(AppAddress::getId,id)
        );
    }
}
